<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-page-header
        content="发布商品"
        style="padding-bottom: 50px"
        @back="goBack"
      />
      <el-form
        ref="form"
        :model="form"
        label-width="120px"
        label-position="right"
        label-suffix="："
        class="form_style"
        :rules="rules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品名称" prop="title">
              <el-input
                v-model="form.title"
                style="width: 400px"
                placeholder="商品名称"
                clearable
              />
            </el-form-item>
            <el-form-item label="商品分类" prop="business_hours">
              <el-select
                v-model="form.value"
                placeholder="请选择商品分类"
                style="width: 200px"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="商品价格" prop="longitude">
              <el-input
                v-model="form.longitude"
                clearable
                style="width: 200px; margin-right: 30px"
                placeholder="商品价格"
              />
            </el-form-item>
            <el-form-item label="商品优惠价格" prop="latitude">
              <el-input
                v-model="form.latitude"
                style="width: 200px"
                placeholder="商品优惠价格"
                clearable
              />
            </el-form-item>
            <el-form-item label="商品描述" prop="announcement">
              <el-input
                v-model="form.announcement"
                type="textarea"
                :rows="3"
                style="width: 400px"
                placeholder="商品描述"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品主图" prop="store_imgs">
              <new-upload
                :file_source="'store_imgs'"
                :upload-type="3"
                :format="'.jpg,.jpeg,.png,.JPG,.JPEG,.PNG'"
                :format-txt="'最多上传1张,支持格式：.jpg,.jpeg,.png,.JPG,.JPEG,.PNG'"
                :limit-num="1"
                @reUpload="getUpladImg"
              />
            </el-form-item>
            <el-form-item label="商品规格" prop="specification_ids">
              <div>
                <el-select v-model="form.specification_ids" placeholder="请选择商品规格">
                  <el-option
                    v-for="item in specification_list"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
                <div class='specification-btn'>
                  <el-button
                    type="success"
                    icon="el-icon-plus"
                    circle
                    size="mini"
                    v-if="specification_ids"
                  ></el-button>
                  <el-button
                    type="danger"
                    icon="el-icon-minus"
                    circle
                    size="mini"
                  ></el-button>
                </div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="footer-btn">
        <el-button type="primary" style="margin-right: 10px" @click="add"
          >提交</el-button
        >
        <el-button @click="goBack">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import NewUpload from "@/components/NewUpload";
import { add_store } from "@/api/store";
export default {
  components: { NewUpload },
  data() {
    return {
      form: {
        store_imgs: [],
      },
      specification_list:[
        {value:'规格1',id:1}
      ],
      rules: {
        title: [{ required: true, message: "请输入商家名称", trigger: "blur" }],
        store_address: [
          { required: true, message: "请输入商家详细地址", trigger: "blur" },
        ],
        longitude: [{ required: true, message: "请输入经度", trigger: "blur" }],
        latitude: [{ required: true, message: "请输入纬度", trigger: "blur" }],
        business_hours: [
          { required: true, message: "请输入营业时间", trigger: "change" },
        ],
        store_phone: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        store_imgs: [
          { required: true, message: "请上传商家图片", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    add() {
      console.log(this.form);
      this.$refs.form.validate((valid) => {
        if (valid) {
          add_store(this.form).then((data) => {
            console.log("data: ", data);
            if (data.code == 200) {
              this.$message.success("添加成功");
              this.goBack();
            } else {
              this.$message.error(data.msg);
            }
          });
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$router.push("/business/list");
    },
    getUpladImg(file_list) {
      console.log("file_list: ", file_list);
      this.form.store_imgs = file_list.join(",");
    },
  },
};
</script>

<style scoped>
.tips {
  margin-left: 120px;
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 14px;
  color: rgb(235, 44, 44);
}
.footer-btn {
  text-align: center;
  margin-top: 50px;
}
.specification-btn{
  display: inline-block;
  padding-left: 20px;
}
</style>
